<?php

namespace app\index\controller;

use think\Controller;
use think\Validate;
use app\index\model\User;

class Regist extends Controller{

    public function index(){

        return $this->fetch();
    }

    public function regist(){
        $user = new User();

        $user->username = input('post.user_name');
        $user->UserEmail = input('post.UserEmail');
        $user->user_password = input('post.UserPasswd');
       
        $result = $this->validate(
            [
                'name' => $user->username,
                'email' => $user->UserEmail,
                'password' => $user->user_password,
            ],
            [
                'name' => 'require|max:10',
                'email' => 'email',
                'password' => 'require',
            ]
            );

            
        if(true !== $result){
            $this->error($result);
        }
        
        if($user->where('username',$user->username)->count() > 0){
            return $this->error('注册失败，用户名已存在');
        }

        if($user->save()){
            return $this->success('注册成功', 'tp5/public/login');
        }else{
            return $this->error('注册失败');
        }
    }
}